//package com.hu.book.convert;
//
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.AuthorityUtils;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
//import org.springframework.util.StringUtils;
//
//import java.util.Collection;
//import java.util.Collections;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
///**
// * @author xh
// */
//public class BookUserAuthenticationConverter extends DefaultUserAuthenticationConverter {
//
//    private static final String USER_ID = "user_id";
//    private static final String N_A = "N/A";
//    private Collection<? extends GrantedAuthority> defaultAuthorities;
//
//    private UserDetailsService userDetailsService;
//
//    /**
//     * Optional {@link UserDetailsService} to use when extracting an {@link Authentication} from the incoming map.
//     *
//     * @param userDetailsService the userDetailsService to set
//     */
//    @Override
//    public void setUserDetailsService(UserDetailsService userDetailsService) {
//        this.userDetailsService = userDetailsService;
//    }
//
//    /**
//     * Default value for authorities if an Authentication is being created and the input has no data for authorities.
//     * Note that unless this property is set, the default Authentication created by {@link #extractAuthentication(Map)}
//     * will be unauthenticated.
//     *
//     * @param defaultAuthorities the defaultAuthorities to set. Default null.
//     */
//    @Override
//    public void setDefaultAuthorities(String[] defaultAuthorities) {
//        this.defaultAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
//                .arrayToCommaDelimitedString(defaultAuthorities));
//    }
//
//    @Override
//    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
//        Map<String, Object> response = new LinkedHashMap<String, Object>();
//        response.put(USERNAME, authentication.getName());
//        if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
//            response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
//        }
//        response.put(USER_ID, "abc");
//        return response;
//    }
//
//
//    private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {
//        if (!map.containsKey(AUTHORITIES)) {
//            return defaultAuthorities;
//        }
//        Object authorities = map.get(AUTHORITIES);
//        if (authorities instanceof String) {
//            return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);
//        }
//        if (authorities instanceof Collection) {
//            return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils
//                    .collectionToCommaDelimitedString((Collection<?>) authorities));
//        }
//        throw new IllegalArgumentException("Authorities must be either a String or a Collection");
//    }
//    @Override
//    public Authentication extractAuthentication(Map<String, ?> map) {
//        if (map.containsKey(USERNAME)) {
//            Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
//
//            String username = (String) map.get(USERNAME);
//            Long id = Long.valueOf((Integer) map.get(USER_ID));
//            if (authorities == null){
//                authorities = Collections.EMPTY_LIST;
//            }
//            SecurityUser user = new SecurityUser(username, N_A, true
//                    , true, true, true, authorities);
//            user.setUserId(id);
//            return new UsernamePasswordAuthenticationToken(user, N_A, authorities);
//        }
//        return null;
//    }
//}
